package huawei.test01;

import java.util.Scanner;
import java.util.Stack;

/**
 * 20230410晚华为机考第三题
 * <p>
 * 题目：解压缩
 * <p>
 * 解法跟求解逆波兰表达式类似
 * <p>
 * 三种规则：
 * 规则一：形如A3，解压为AAA，单个字符后直接跟数字
 * 规则二：形如{AB}2，解压为ABAB，括号内为字符串，括号外带数字
 * 规则三：形如{A3B1{C}2}3，解压为AAABCCAAABCCAAABCC，括号嵌套
 * <p>
 * 区分大小写
 * 数字可能存在多位数 （一开始没有这一点导致通过率只有20%）
 * <p>
 * 测试用例：输入 {A3B1{C}2}3  输出 AAABCCAAABCCAAABCC
 */
public class t3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String compressedString = in.nextLine();
        char[] charArray = compressedString.toCharArray();
        Stack<String> stack = new Stack<>();
        String num = "";
        for (int i = 0; i < charArray.length; i++) {
            char curChar = charArray[i];
            if (curChar == '{' || curChar == '}') {
                stack.push(String.valueOf(curChar));
            } else if (isAlphabeta(curChar)) {
                stack.push(String.valueOf(curChar));
            } else if (isNumber(curChar)) {
                num = num + curChar;
                // 如果下一位还是数字，则先跳过
                if (i < charArray.length - 1 && isNumber(charArray[i + 1])) {
                    continue;
                }
                if (stack.peek().length() == 1 && !stack.peek().equals("}")) {
                    String pop = stack.pop();
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < Integer.parseInt(num); j++) {
                        sb.append(pop);
                    }
                    stack.push(sb.toString());
                } else if (stack.peek().length() == 1 && stack.peek().equals("}")) {
                    stack.pop();
                    StringBuilder sb = new StringBuilder();
                    while (!stack.peek().equals("{")) {
                        sb.insert(0, stack.pop());
                    }
                    stack.pop();
                    String temp = sb.toString();
                    for (int j = 1; j < Integer.parseInt(num); j++) {
                        sb.append(temp);
                    }
                    stack.push(sb.toString());
                }
                num = "";
            } else {
                stack.push(String.valueOf(curChar));
            }
        }
        System.out.println(stack.pop());
    }

    private static boolean isNumber(char curChar) {
        return curChar >= '0' && curChar <= '9';
    }

    private static boolean isAlphabeta(char curChar) {
        return curChar >= 'a' && curChar <= 'z' || curChar >= 'A' && curChar <= 'Z';
    }
}
